package com.prime.tutorial.moviestore.dao;


import com.prime.tutorial.moviestore.domain.Groups;
import com.prime.tutorial.moviestore.domain.Users;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.transaction.TransactionConfiguration;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

@RunWith(SpringJUnit4ClassRunner.class)
@Transactional
@TransactionConfiguration(transactionManager = "txManager")
@ContextConfiguration(locations = {"/applicationContext.xml"})
public class UserDAOWithJPATest {

    private UserDAO userDAO;

    @Autowired
    public void setRepository(UserDAO userDAO) {
        this.userDAO = userDAO;
    }

    @Test
    public void shouldPersistNewUser() {
        Users users = new Users();
        users.setCreated(new Date());
        users.setDescription("TestUser1");
        users.setEnabled(true);
        users.setFirstName("FirstTest1Name");
        users.setGroup(new Groups("Test1"));
        users.setLastName("LastTest1Name");
        users.setLogin("testlogin");
        users.setPassword("testPass");
        users.setValidity(new Date());

        userDAO.persist(users);

        assertNotNull(users.getId());
    }

    @Test
    public void shouldFindByTitle() {
        Users users = new Users();
        users.setCreated(new Date());
        users.setDescription("TestUser2");
        users.setEnabled(false);
        users.setFirstName("FirstTest2Name");
        users.setGroup(new Groups("Test2"));
        users.setLastName("LastTest2Name");
        users.setLogin("testlogin2");
        users.setPassword("testPass2");
        users.setValidity(new Date());

        userDAO.persist(users);
        assertNotNull(users.getId());

        List<Users> results = userDAO.findByLogin("testlogin2");
        assertEquals(1, results.size());
        assertEquals("testlogin2", results.get(0).getLogin());
    }

    @Test
    public void shouldReadAllUsers() {
        Users user1 = new Users();
        user1.setCreated(new Date());
        user1.setDescription("TestUser1");
        user1.setEnabled(true);
        user1.setFirstName("FirstTest1Name");
        user1.setGroup(new Groups("Test1"));
        user1.setLastName("LastTest1Name");
        user1.setLogin("testlogin");
        user1.setPassword("testPass");
        user1.setValidity(new Date());


        Users user2 = new Users();
        user2.setCreated(new Date());
        user2.setDescription("TestUser2");
        user2.setEnabled(false);
        user2.setFirstName("FirstTest2Name");
        user2.setGroup(new Groups("Test2"));
        user2.setLastName("LastTest2Name");
        user2.setLogin("testlogin2");
        user2.setPassword("testPass2");
        user2.setValidity(new Date());

        userDAO.persist(user1);
        assertNotNull(user1.getId());

        userDAO.persist(user2);
        assertNotNull(user2.getId());

        List<Users> results = userDAO.loadAll();
        assertEquals(2, results.size());
    }
}